package com.me.common.util;

import java.time.LocalTime;

/**
 * JDK8 日期工具类
 *
 * @author ME
 * @date 2021/2/22
 */
public class LocalTimeUtils {
    /** 每天包含的 分钟数 */
    public static final long MINUTES_OF_DAY = 1440;
    /** 每天包含的 秒数 */
    public static final long SECONDS_OF_DAY = 86400;
    /** 每分钟包含的 秒数 */
    public static final long SECONDS_PER_MINUTE = 60;
    /** 每秒包含的 毫秒数 */
    public static final long MILLIS_PER_SECOND = 1000L;
    /** 每秒包含的 微妙数 */
    public static final long MICROS_PER_SECOND = 1_000_000L;
    /** 每秒包含的 纳秒数 */
    public static final long NANOS_PER_SECOND = 1_000_000_000L;
    /** 每分钟包含的 纳秒数 */
    public static final long NANOS_PER_MINUTE = NANOS_PER_SECOND * SECONDS_PER_MINUTE;

    /** 以分钟为单位，向上取整，如：23:59:59.999999999 取整为 00:00:00 */
    public static LocalTime ceilMinutes(LocalTime time) {
        if (time.getSecond() > 0 || time.getNano() > 0) {
            long secondOfDay = (time.toSecondOfDay() / SECONDS_PER_MINUTE + 1) * SECONDS_PER_MINUTE;
            if (secondOfDay > SECONDS_OF_DAY - SECONDS_PER_MINUTE) {
                secondOfDay = 0;
            }
            time = LocalTime.ofSecondOfDay(secondOfDay);
        }
        return time;
    }

    /** 以秒为单位，向上取整，如：23:59:59.999999999 取整为 00:00:00 */
    public static LocalTime ceilSeconds(LocalTime time) {
        if (time.getNano() > 0) {
            time = LocalTime.ofSecondOfDay(time.toSecondOfDay()).plusSeconds(1);
        }
        return time;
    }

    public static void main(String[] args) {
        System.out.println(ceilSeconds(LocalTime.MAX));
        System.out.println(ceilMinutes(LocalTime.MAX));
        System.out.println(ceilMinutes(LocalTime.of(23,58,1)));
    }

}
